package com.itheima.service.backend.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.backend.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.backend.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 检查项服务实现类
 */
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 新增检查项
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        log.info("新增检查项：{}",checkItem);
        checkItemMapper.add(checkItem);

    }

    /**
     * 删除检查项
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        log.info("删除检查项：{}",id);
        if(checkItemMapper.findCheckGroudByCheckItemId(id) != null){
            return new Result(false,"该检查项被检查组关联，不能删除");
        }else {
            checkItemMapper.delete(id);
            return new Result(true, "删除检查项成功");
        }
    }

    /**
     * 通过检查组id查询检查项id集合
     * @param checkgroupId
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        return checkItemMapper.findCheckItemIdsByCheckGroupId(checkgroupId);
    }

    /**
     * 编辑检查项
     * @param checkItem
     */
    @Override
    public Result edit(CheckItem checkItem) {
        log.info("编辑检查项：{}", checkItem);
        if(checkItemMapper.findCheckGroudByCheckItemId(checkItem.getId()) != null){
            return new Result(false, "该检查项被检查组关联，不能修改");
        }else {
            checkItemMapper.edit(checkItem);
            return new Result(true, "修改检查项成功");
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("分页查询：{}", queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Object> page = checkItemMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 查询所有检查项
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        log.info("查询所有检查项");
        return checkItemMapper.findAll();
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        log.info("根据id查询检查项：{}", id);
        return checkItemMapper.findById(id);
    }
}
